<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/5
 * Time: 9:11
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Banner extends Base {

    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['banner_title'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($banner_status) && $banner_status != ''){
                $where['banner_status'] = $banner_status;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('banner')->where($where)->count();// 获取总条数
            $lists = Db::name('banner')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('banner_sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 200, 'msg' => '上架失败']);
        }
    }

    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $banner_id)->setField('banner_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 200, 'msg' => '下架失败']);
        }
    }

    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'banner_sort'=>input('value'),
        ];
        $res = Db::name('banner')->where('banner_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del(){
        $id = input('id');
        $res = Db::table('up_banner')->where('banner_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 添加banner
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(Request::instance()->isPost()){
            $banner_title = trim(input('banner_title'), ' ');
            $banner_img = input('banner_img');
            $banner_status = input('banner_status');
            $banner_type = input('banner_type');
            $banner_sort = input('banner_sort');
//            $post_id = input('post_id');
            if($banner_type==1){
                $post_id = input('post_id1');
            }
            if($banner_type==2){
                $post_id = input('post_id2');
            }
            if($banner_type==3){
                $post_id = input('post_id3');
            }
            if($banner_type==4){
                $post_id = input('post_id4');
            }
            $data = [
                'banner_title' => $banner_title,
                'banner_img' => $banner_img,
                'banner_status' => $banner_status,
                'banner_type' => $banner_type,
                'banner_time' => date('Y-m-d H:i:s'),
                'banner_sort' =>$banner_sort,
                'banner_link' =>$post_id
            ];
            $res = Db::table('up_banner')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        //热门老师
        $where['user_type'] = 2;
        $where['user_status'] = 2;
        $res1 = Db::table('up_u_user')->where($where)
            ->field('user_id,user_username')
            ->select();
        //比赛消息
        $where1['match_status'] = 1;
        $res2 = Db::table('up_match')->where($where1)
            ->select();
        //公开课
        $where2['is_hot'] = 1;
        $res3 = Db::table('up_public_class')->where($where2)
            ->select();
        //题库
        $where3['question_status'] = 1;
        $res4 = Db::table('up_question_bank')->where($where3)
            ->select();
        $this->assign('res1',$res1);
        $this->assign('res2',$res2);
        $this->assign('res3',$res3);
        $this->assign('res4',$res4);
        return $this->fetch();
    }


    /**
     * 编辑轮播图
     * @return mixed|\think\response\Json
     */
    public function edit(){
        if(Request::instance()->isPost()){
            $id = input('banner_id');
            $banner_title = trim(input('banner_title'), ' ');
            $banner_img = input('banner_img');
            $banner_status = input('banner_status');
            $banner_type = input('banner_type');
            $banner_sort = input('banner_sort');
            if($banner_type==1){
                $post_id = input('post_id1');
            }
            if($banner_type==2){
                $post_id = input('post_id2');
            }
            if($banner_type==3){
                $post_id = input('post_id3');
            }
            if($banner_type==4){
                $post_id = input('post_id4');
            }
            $data = [
                'banner_title' => $banner_title,
                'banner_status' => $banner_status,
                'banner_type' => $banner_type,
                'banner_sort' =>$banner_sort,
                'banner_link' =>$post_id
            ];

            if(!empty($banner_img)){
                $data['banner_img'] = $banner_img;
            }

            $res = Db::table('up_banner')->where('banner_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $banner_id = input('banner_id');
        // banner
        $banner = Db::table('up_banner')
            ->where('banner_id', $banner_id)
            ->find();
        //热门老师
        $where['user_type'] = 2;
        $where['user_status '] = 2;
        $res1 = Db::table('up_u_user')->where($where)
            ->field('user_id,user_username')
            ->select();
        //比赛消息
        $where1['match_status'] = 1;
        $res2 = Db::table('up_match')->where($where1)
            ->select();
        //公开课
        $where2['is_hot'] = 1;
        $res3 = Db::table('up_public_class')->where($where2)
            ->select();
        //题库
        $where3['question_status'] = 1;
        $res4 = Db::table('up_question_bank')->where($where3)
            ->select();
        $this->assign('res1',$res1);
        $this->assign('res2',$res2);
        $this->assign('res3',$res3);
        $this->assign('res4',$res4);
        $this->assign([
            'banner_id' => $banner_id,
            'banner' => $banner
        ]);
        return $this->fetch();
    }
}